package com.yuchen.ishop.order;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.yuchen.core.MyPager;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.ishop.MyPagerUtils;
import com.yuchen.ishop.entity.Order;
import com.yuchen.jaxrs.ResponseUtils;
import com.yuchen.jaxrs.Responses;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Transactional
@Path("orderService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class OrderServiceImpl implements OrderService {

    @Autowired
    private BaseDao dao;

    @POST
    @Path("insert")
    public Order insert(Order order) {
        return dao.insert(order);
    }

    @GET
    @Path("findByOrderNo/{orderNo}")
    @Transactional(readOnly = true)
    public List<Order> findByOrderNo(@PathParam("orderNo") String orderNo) {
        return dao.query(Order.class, Cnd.where("orderNo","=",orderNo));
    }

    @POST
    @Path("findBySeller")
    @Transactional(readOnly = true)
    public MyPager findBySeller(Map param) {
        MyPager myPager = MyPagerUtils.myPager(param);
        StringBuilder sql = new StringBuilder();
        sql.append("select t1.id ,t1.create_time, t1.price price,t1.real_price pay, t1.order_no, t1.sub_order_no,t1.state,t1.buyer_id, ");
        sql.append("t2.name, t2.price ,t3.image_url,t2.id spid,t2.seller_xiaoer_id xiao_id,t2.seller_id seller_id ");
        sql.append("from t_orders t1, t_goods t2, t_goods_imgs t3 ");
        sql.append("where t1.goods_id = t2.id and t2.id = t3.goods_id and t3.type = 1 ");
        sql.append("and t1.seller_id = @sellerId ");
        if(param.get("xiaoerId")!=null){
            sql.append("and t2.seller_xiaoer_id = @xiaoerId ");
        }
        return dao.queryPager(sql.toString(),param,myPager);
    }

    @POST
    @Path("changeOrderPrice")
    public Responses changeOrderPrice(Map param) {
        Chain chain = Chain.make("realPrice",param.get("realPrice"));
        dao.update(Order.class, chain, Cnd.where("subOrderNo", "=", param.get("subOrderNo")));
        return ResponseUtils.success();
    }
}
